/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gui;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

import rmiclient.Service;

/**
 * 
 * @author salim
 */
public class ServerPropertiesDialog extends javax.swing.JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * A return status code - returned if Cancel button has been pressed
	 */
	public static final int RET_CANCEL = 0;
	/**
	 * A return status code - returned if OK button has been pressed
	 */
	public static final int RET_OK = 1;

	private DialogType dialogType = DialogType.SERVER_PROPERTIES_DIALOG;

	/**
	 * Creates new form ServerPropertiesDialog
	 */
	public ServerPropertiesDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();

		// Close the dialog when Esc is pressed
		String cancelName = "cancel";
		InputMap inputMap = getRootPane().getInputMap(
				JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
		ActionMap actionMap = getRootPane().getActionMap();
		actionMap.put(cancelName, new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent e) {
				doClose(RET_CANCEL);
			}
		});
	}

	/**
	 * @return the return status of this dialog - one of RET_OK or RET_CANCEL
	 */
	public int getReturnStatus() {
		return returnStatus;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */

	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		serverLocationTextField = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		portNumberTextField = new javax.swing.JTextField();
		jPanel2 = new javax.swing.JPanel();
		okButton = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();

		addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent evt) {
				closeDialog(evt);
			}
		});

		jLabel1.setText("Server location");
		jPanel1.add(jLabel1);

		serverLocationTextField
				.setPreferredSize(new java.awt.Dimension(100, 30));
		serverLocationTextField
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						serverLocationTextFieldActionPerformed(evt);
					}
				});
		jPanel1.add(serverLocationTextField);

		jLabel2.setText("Port number");
		jPanel1.add(jLabel2);

		portNumberTextField.setPreferredSize(new java.awt.Dimension(100, 30));
		portNumberTextField
				.addActionListener(new java.awt.event.ActionListener() {
					@Override
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						serviceNameTextFieldActionPerformed(evt);
					}
				});
		jPanel1.add(portNumberTextField);

		getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

		okButton.setText("OK");
		okButton.setPreferredSize(new java.awt.Dimension(100, 30));
		okButton.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				okButtonActionPerformed(evt);
			}
		});
		jPanel2.add(okButton);
		getRootPane().setDefaultButton(okButton);

		cancelButton.setText("Cancel");
		cancelButton.setPreferredSize(new java.awt.Dimension(100, 30));
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});
		jPanel2.add(cancelButton);

		getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

		serverLocationTextField.setText(Service.getServerLocation());
		portNumberTextField.setText(String.valueOf(Service.getPort()));

		pack();
	}// </editor-fold>

	private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
		Service.setServiceProperties(
				Integer.valueOf(portNumberTextField.getText()).intValue(),
				serverLocationTextField.getText());
		doClose(RET_OK);
	}

	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
		doClose(RET_CANCEL);
	}

	/**
	 * Closes the dialog
	 */
	private void closeDialog(java.awt.event.WindowEvent evt) {
		doClose(RET_CANCEL);
	}

	private void serverLocationTextFieldActionPerformed(
			java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void serviceNameTextFieldActionPerformed(
			java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
	}

	private void doClose(int retStatus) {
		returnStatus = retStatus;
		setVisible(false);
		dispose();
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(
					ServerPropertiesDialog.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(
					ServerPropertiesDialog.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(
					ServerPropertiesDialog.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(
					ServerPropertiesDialog.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the dialog */
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ServerPropertiesDialog dialog = new ServerPropertiesDialog(
						new javax.swing.JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					@Override
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	/**
	 * @return the dialogType
	 */
	public DialogType getDialogType() {
		return dialogType;
	}

	/**
	 * @param dialogType
	 *            the dialogType to set
	 */
	public void setDialogType(DialogType dialogType) {
		this.dialogType = dialogType;
	}

	// Variables declaration - do not modify
	private javax.swing.JButton cancelButton;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JButton okButton;
	private javax.swing.JTextField serverLocationTextField;
	private javax.swing.JTextField portNumberTextField;
	// End of variables declaration

	private int returnStatus = RET_CANCEL;
}
